/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  SRVConnectionViewController.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "iPhoneSRVAppDelegate.h"
#import "SRVConnectionViewController.h"
#import "SRVUIVCResetConnection.h"

@implementation SRVConnectionViewController
@synthesize activity, leftBlackfin, rightBlackfin, logDisplay, okButton, retryButton, configuration;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	[okButton setHidden:YES];
	[retryButton setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];	
}

-(void)clearObject {
	activity = nil;
	leftBlackfin = nil;
	rightBlackfin = nil;
	leftProbe = nil;
	rightProbe = nil;
	logDisplay = nil;
	probingTimer = nil;
	okButton = nil;
	log = nil;
}

- (void)viewDidUnload {
	[self clearObject];
	[super viewDidUnload];
}

- (void)dealloc {
	[self clearObject];
    [super dealloc];
}

-(IBAction)retryButton:(id)sender {
	NSLog(@"Retry button pressed");
	if( leftProbe.ipConnected==YES )
		[leftBlackfin closeConnection:nil];
	
	if( rightProbe.ipConnected==YES )
		[rightBlackfin closeConnection:nil];
		
	[self startConnectionProbe];
}



-(void)startConnectionProbe {
	[okButton setHidden:YES];
	[retryButton setHidden:YES];	
	[activity startAnimating];

	NSString *releaseLog = log;
	log = [[NSMutableString alloc]initWithCapacity:1024];
	
	[log setString:@""];
	
	NSString *version = [NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
	
	[log appendFormat:@"iPhoneSRV v%@\n", version];
	[logDisplay setText:log];
	[logDisplay setNeedsDisplay];
		
	[leftBlackfin setTargetURL:[configuration ipAddress]];
	[leftBlackfin setPortNumber:[configuration portNumberUART0]];
	[rightBlackfin setTargetURL:[configuration ipAddress]];
	[rightBlackfin setPortNumber:[configuration portNumberUART1]];
	
	[log appendString:@"Establishing connections\n"];
	[log appendString:@"Probe timeout: 5 seconds\n"];
	[log appendFormat:@"SRV IP: %@\n", [leftBlackfin targetURL]];
	[log appendFormat:@"Probing Ports %i & %i\n\n", [leftBlackfin portNumber],[rightBlackfin portNumber]];

	probingTimer = [NSTimer scheduledTimerWithTimeInterval:5
												target:self
												  selector:@selector(probingExpired:)
											  userInfo:nil
												   repeats:NO];
	
	leftProbe=[[SRVUICVCConnectProbe alloc] init:leftBlackfin];
	rightProbe=[[SRVUICVCConnectProbe alloc] init:rightBlackfin];
	
	[logDisplay setText:log];
	[logDisplay setNeedsDisplay];
	if(releaseLog!=nil)
		[releaseLog release];
} 

-(void)probingExpired:(NSTimer*)theTimer {
	[activity stopAnimating];
	[log appendFormat:@"%i: %@\n", [leftBlackfin portNumber], [leftProbe bfVersionString]]; 		
	[log appendFormat:@"%i: %@\n", [rightBlackfin portNumber], [rightProbe bfVersionString]]; 
	
	[logDisplay setText:log];
	[logDisplay setNeedsDisplay];
	
	NSLog(@"%i: %@\n", [leftBlackfin portNumber], [leftProbe bfVersionString]); 
	NSLog(@"%i: %@\n", [rightBlackfin portNumber], [rightProbe bfVersionString]);
	
	BOOL okHidden = YES;
	if( leftProbe.ipConnected && leftProbe.bfPresent )
		okHidden=NO;
	if( rightProbe.ipConnected && rightProbe.bfPresent )
		okHidden=NO;
	[okButton setHidden:okHidden];

	BOOL retryHidden = YES;
	if( !(leftProbe.ipConnected && leftProbe.bfPresent) )
		retryHidden=NO;
	if( !(rightProbe.ipConnected && rightProbe.bfPresent) )
		retryHidden=NO;
	[retryButton setHidden:retryHidden];

	probingTimer = nil;
	leftProbe = nil;
	rightProbe = nil;
}

@end

